
;; (add-to-list 'company-backends #'company-tabnine)

(defadvice company-echo-show (around disable-tabnine-upgrade-message activate)
  (let ((company-message-func (ad-get-arg 0)))
    (when (and company-message-func
               (stringp (funcall company-message-func)))
      (unless (string-match "The free version of TabNine only indexes up to" (funcall company-message-func))
        ad-do-it))))

(defvar *lutts-disable-company-modes*
  '(realgud-track-mode-hook
    gdb-mode-hook))

(defun lutts-disable-company nil
  (setq-local company-idle-delay nil))

(defun lutts-toggle-company-auto-complete nil
  "A command to toggle company auto complete."
  (interactive)
  (if (not company-idle-delay)
      (progn
       (message "Company auto completion enabled.")
       (setq-local company-idle-delay 0))
    (progn
     (message "Company auto completion disabled.")
     (setq-local company-idle-delay nil))))

(defun lutts-init-company nil
  (require 'company)
  (require 'company-posframe)
  (setq
   company-idle-delay nil
   company-show-numbers t)

  (global-set-key (kbd "C-c q") 'lutts-toggle-company-auto-complete)
  (global-set-key (kbd "C-q") 'company-complete)
  (global-set-key (kbd "M-q") 'company-tabnine-call-other-backends)

  (dolist (i *lutts-disable-company-modes*)
    (add-hook i #'lutts-disable-company))
  (add-hook 'after-init-hook 'global-company-mode)
  (add-hook 'after-init-hook 'company-posframe-mode))

(lutts-load-package 'lutts-init-company 'company 'company-posframe)


(provide 'init-company)
